$(function(){

    function popFn(dataType , data){
        popHtml = ''
        popHtml += '<div id="graybg_pop" class="graybg_pop ie6fixed_t" style="display:block"></div>'
        popHtml += '<div class="col-lg-3 teacher_pop banner_pop" style="display:boock">'
        popHtml += '    <span class="teacher_tit">添加</span>'
        popHtml += '    <i class="fa fa-times" aria-hidden="true"></i>'
        popHtml += '    <div class="big-photo">'
        popHtml += '        <div id="filePicker">'
        popHtml += '        </div>'
        popHtml += '        <div class="viewThumb">'
        if(dataType == 1){
            popHtml += '            <img id="imghead" border="0" src="'+ data.img_url +'" data_src = '+ data.img_url +' width="470" height="200" >'
        }else{
            popHtml += '            <img id="imghead" border="0" src="images/photo_icon.png" data_src = "images/photo_icon.png" width="470" height="200" >'
        }
        popHtml += '        </div>'
        popHtml += '    </div>'
        popHtml += '    <p class="p_btn"><button type="button" class="btn btn-lg btn-success ok_success" data_type="1">确定</button></p>'
        popHtml += '</div>'
    }
    //名师列表
    admin.ajaxFn(
        {
            url:admin.baseUrl.bannerList,
            type : 'GET',
            data : {
                page_size : 20,
                page : 1
            }
        },
        function(data){
            $('#page-wrapper').html(template('teacherTpl',{'res': data}));
        }
    )

    //修改
    $('body').on( 'click' ,'.fa-pencil-square' , function(){
        var dataType = $(this).attr('data_type')
        var dataid = $(this).attr('data_id')
        admin.ajaxFn(
            {
                url:admin.baseUrl.bannerListMod,
                type : 'GET',
                data : {
                    id : dataid
                }
            },
            function(data){
                popFn(dataType , data)
                $('body').append(popHtml)
                $('.teacher_tit').text('修改')
                $('.btn-success').attr({'data_type' : '1', 'data_id' : dataid})
                admin.webuploaderFn('点击上传', admin.baseUrl.bannerListUpdate, 'Images', 'gif,jpg,jpeg,png', 'image/gif,image/jpg,image/jpeg,image/png')
                admin.isloader = false;
            }
        )
    })

    //更新
    $('body').on( 'click' ,'.ok_success' , function(){
        var dataType = $(this).attr('data_type')
        var dataid = $(this).attr('data_id')
        if(admin.isloader) {
            layer.msg('请选择图片')
            return false
        }else{
            if(dataType == 1){
                admin.uploader.options.formData.token = admin.getCookierl('cookval');
                admin.uploader.options.formData.id_b = dataid
                admin.uploader.on('uploadSuccess', function (file, response) {
                    admin.ajaxFn(
                        {
                            url: admin.baseUrl.bannerList,
                            type: 'GET',
                            data: {
                                page_size: 20,
                                page: 1
                            }
                        },
                        function (data) {
                            $('#graybg_pop , .teacher_pop').remove();
                            admin.isloader = true;
                            layer.msg('修改成功')
                            $('#page-wrapper').html(template('teacherTpl',{'res': data}));
                        }
                    )
                });
                admin.uploader.on('uploadComplete', function (file) {
                });
                if (admin.uploader.state === 'ready') {
                    admin.uploader.upload();
                } else if (admin.uploader.state === 'paused') {
                    admin.uploader.upload();
                } else if (admin.uploader.state === 'uploading') {
                    admin.uploader.stop();
                }
            }else{
                admin.uploader.options.formData.token = admin.getCookierl('cookval');
                admin.uploader.on('uploadSuccess', function (file, response) {
                    admin.ajaxFn(
                        {
                            url: admin.baseUrl.bannerList,
                            type: 'GET',
                            data: {
                                page_size: 20,
                                page: 1
                            }
                        },
                        function (data) {
                            $('#graybg_pop , .teacher_pop').remove();
                            admin.isloader = true;
                            layer.msg('添加成功')
                            $('#page-wrapper').html(template('teacherTpl',{'res': data}));
                        }
                    )
                });
                admin.uploader.on('uploadComplete', function (file) {
                });
                if (admin.uploader.state === 'ready') {
                    admin.uploader.upload();
                } else if (admin.uploader.state === 'paused') {
                    admin.uploader.upload();
                } else if (admin.uploader.state === 'uploading') {
                    admin.uploader.stop();
                }
            }
        }
    })



    //增加
    $('body').on( 'click' ,'.add_list' , function(){
        var dataType = $(this).attr('data_type')
        var dataid = $(this).attr('data_id')
        popFn(dataType , '')
        $('body').append(popHtml)
        $('.teacher_tit').text('添加')
        $('.btn-success').attr({'data_type' : '2', 'data_id' : dataid})
        admin.webuploaderFn('点击上传', admin.baseUrl.bannerListAdd, 'Images', 'gif,jpg,jpeg,png', 'image/gif,image/jpg,image/jpeg,image/png')
        admin.isloader = false;
    })
    //删除
    $('body').on( 'click' ,'.fa-minus-square' , function(){
        var dataid = $(this).attr('data_id')
        layer.open({
            type: 1
            ,title: false //不显示标题栏
            ,closeBtn: false
            ,area: '300px;'
            ,shade: 0.8
            ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
            ,btn: ['确定', '取消']
            ,btnAlign: 'c'
            ,moveType: 1 //拖拽模式，0或者1
            ,content: '<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">确定要删除吗？</div>'
            ,yes: function(){
                $('.layui-layer-shade, .layui-layer').remove();
                admin.ajaxFn(
                    {
                        url:admin.baseUrl.bannerListRemove,
                        type : 'GET',
                        data : {
                            id : dataid
                        }
                    },
                    function(data){
                        alert('删除成功！')
                        //名师列表
                        admin.ajaxFn(
                            {
                                url:admin.baseUrl.bannerList,
                                type : 'GET',
                                data : {
                                    page_size : 10,
                                    page : 1,
                                    order_sn : '5745412',
                                    serial_number : '897845',
                                    status : '1'
                                }
                            },
                            function(data){
                                $('#page-wrapper').html(template('teacherTpl',{'res': data}));
                            }
                        )
                    }
                )
            }

        });

    })

   
    // //上传文件
    // $('body').on( 'click' ,'#imghead' , function(){
    //     $('#previewImg').click();
    // })

})



// function previewImage(file)
//     {
//       var MAXWIDTH  = 470;
//       var MAXHEIGHT = 235;
//       var div = document.getElementById('preview');
//       if (file.files && file.files[0])
//       {
//           div.innerHTML ='<img id=imghead onclick=$("#previewImg").click();>';
//           var img = document.getElementById('imghead');
//           img.onload = function(){
//             var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
//             img.width  =  rect.width;
//             img.height =  rect.height;
//             //img.style.marginLeft = rect.left+'px';
//             img.style.marginTop = rect.top+'px';
//           }
//           var reader = new FileReader();
//           reader.onload = function(evt){img.src = evt.target.result;}
//           reader.readAsDataURL(file.files[0]);
//       }
//       else //兼容IE
//       {
//         var sFilter='filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="';
//         file.select();
//         var src = document.selection.createRange().text;
//         div.innerHTML = '<img id=imghead>';
//         var img = document.getElementById('imghead');
//         img.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
//         var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
//         status =('rect:'+rect.top+','+rect.left+','+rect.width+','+rect.height);
//         div.innerHTML = "<div id=divhead style='width:"+rect.width+"px;height:"+rect.height+"px;margin-top:"+rect.top+"px;"+sFilter+src+"\"'></div>";
//       }
//     }
//     function clacImgZoomParam( maxWidth, maxHeight, width, height ){
//         var param = {top:0, left:0, width:width, height:height};
//         if( width>maxWidth || height>maxHeight ){
//             rateWidth = width / maxWidth;
//             rateHeight = height / maxHeight;

//             if( rateWidth > rateHeight ){
//                 param.width =  maxWidth;
//                 param.height = Math.round(height / rateWidth);
//             }else{
//                 param.width = Math.round(width / rateHeight);
//                 param.height = maxHeight;
//             }
//         }
//         param.left = Math.round((maxWidth - param.width) / 2);
//         param.top = Math.round((maxHeight - param.height) / 2);
//         return param;
//     }